package com.xq.jltx.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xq.jltx.db.bean.Note;
import com.xq.jltx.db.dao.NoteDao;

import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Date;
import java.util.List;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/19.
 */
public class NoteDbUtils {

    /**
     *插入新的一条Note数据到数据库
     * @param note
     */
    public static void addNote(Note note){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao =  manager.getDaoSession().getNoteDao();
        noteDao.insert(note);
    }

    /**
     * 批量插入Note数据到数据库
     * @param noteList
     */
    public static void addNoteTx(List<Note> noteList){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao =  manager.getDaoSession().getNoteDao();
        noteDao.insertInTx((Note[])noteList.toArray(new Note[noteList.size()]));
    }
    /**
     * 获取数据库的所有Note的数据
     * @return
     */
    public static List<Note> getAllNotes(){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao =  manager.getDaoSession().getNoteDao();
        return noteDao.loadAll();
    }

    /**
     * 获取数据库的所有Note的数据Cursor
     * @return
     */
    public static Cursor getAllNotesCursor(){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        SQLiteDatabase db = manager.getDb();
        String ageCoulmn =NoteDao.Properties.Date.columnName;
        String  orderby = ageCoulmn + "  COLLATE LOCALIZED DESC";
        NoteDao noteDao =  manager.getDaoSession().getNoteDao();
//        Cursor cursor  = db.query(noteDao.getTablename(),noteDao.getAllColumns(),
//                null,null,null,null,orderby);
        Cursor cursor = db.rawQuery(SqlUtils.createSqlSelect(noteDao.getTablename(), "T", noteDao.getAllColumns(), false) + " ORDER BY "+orderby,null);

        return cursor;
    }
    /**
     * 查询某个表是否有text
     * @param text
     * @return
     */
    public static boolean isSaved(String text){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao =  manager.getDaoSession().getNoteDao();
        QueryBuilder<Note> noteQueryBuilder = noteDao.queryBuilder();
        long count =  noteQueryBuilder.where(NoteDao.Properties.Text.eq(text))
                .orderAsc(NoteDao.Properties.Date)
                .buildCount().count();
        return count > 0 ? true : false;
    }

    /**
     * 查询某个表里有text的所有数据
     * @param text
     * @return
     */
    public static List<Note> getNotesFromText(String text) {
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao = manager.getDaoSession().getNoteDao();
        QueryBuilder<Note> noteQueryBuilder = noteDao.queryBuilder();
        List<Note> noteList = noteQueryBuilder.where(NoteDao.Properties.Text.eq(text))
                .orderAsc(NoteDao.Properties.Date)
                .build().list();
        return noteList;
    }

    /**
     * 模糊查询数据库
     * @param text
     * @return
     */
    public static List<Note> getNotesFromTextLike(String text) {
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao = manager.getDaoSession().getNoteDao();
        QueryBuilder<Note> noteQueryBuilder = noteDao.queryBuilder();
        List<Note> noteList = noteQueryBuilder.where(NoteDao.Properties.Text.like("%"+text+"%"))
                .orderDesc(NoteDao.Properties.Date)
                .build().list();
        return noteList;
    }
    /**
     * 删除其中一个匹配的Note数据
     * @param text
     */
    public static void deleteNote(String text,String comment){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao = manager.getDaoSession().getNoteDao();
        QueryBuilder<Note> noteQueryBuilder = noteDao.queryBuilder();
        List<Note> noteList = noteQueryBuilder.where(NoteDao.Properties.Text.eq(text),NoteDao.Properties.Comment.eq(comment)).build().list();
        if(noteList != null && noteList.size() > 0){
            noteDao.delete(noteList.get(0));
        }
    }

    /**
     * 通过主键删除Note数据
     * @param id
     */
    public static void deleteNote(long id){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao = manager.getDaoSession().getNoteDao();
        noteDao.deleteByKey(id);
    }
    /**
     * 修改数据
     * @param text
     * @param comment
     */
    public static void modefyNote(long id,String text,String comment,Date date){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao = manager.getDaoSession().getNoteDao();
        Note note = new Note(id,text,comment,date);
        noteDao.update(note);
    }

    /**
     *修改Note数据
     * @param note
     */
    public static void modefyNote(Note note){
        GreenDaoDBManager manager = GreenDaoDBManager.getInstance();
        NoteDao noteDao = manager.getDaoSession().getNoteDao();
        noteDao.update(note);
    }

    /**
     * 通过cursor获取一个Note对象
     * @param cursor
     * @return
     */
    public static Note getNoteFromCursor(Cursor cursor){
        Note note = null;
        if(cursor != null && !cursor.isClosed()){
            note = new Note();
            note.setId(cursor.getLong(cursor.getColumnIndex(NoteDao.Properties.Id.columnName)));
            note.setText(cursor.getString(cursor.getColumnIndex(NoteDao.Properties.Text.columnName)));
            note.setComment(cursor.getString(cursor.getColumnIndex(NoteDao.Properties.Comment.columnName)));
            note.setDate(new Date(cursor.getLong(cursor.getColumnIndex(NoteDao.Properties.Date.columnName))));
        }
        return note;
    }
}
